<template> 	
  <div class="page">
	<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">
						
						<div class="layui-card-body">							
							<div class="layui-form" lay-filter="">																																												    						    						
								<div class="layui-form-item" v-show="isfrom==1" style="display:none;">
									<div class="layui-inline">
									    <label class="layui-form-label">客户名称</label>
									    <div class="layui-input-inline">
												<select name="accountId" lay-filter="accountId" v-model="accountId">
													<option v-for="item in khlist" :value="item.id">{{item.nickName}}</option>													 										  
												</select>
											</div>											   
								  </div>
								</div>
								<div class="layui-form-item" v-show="isfrom==2" style="display:none;">
									<div class="layui-inline">
									    <label class="layui-form-label">更变选项</label>
									    <div class="layui-input-inline">
												<select name="aid" lay-filter="aid" v-model="aid">
													  <option value="1">更变上级</option>
													  <!--<option value="2">更换下级</option>-->
												</select>
											</div>											   
								  </div>
								</div>
								<div class="layui-form-item" v-show="isfrom==2" style="display:none;">
									<div class="layui-inline">
									    <label class="layui-form-label">当前上级</label>
									    <div class="layui-input-inline" >
									        <input type="text" name="referrer" disabled="disabled" placeholder=""  v-model="itemData.referrer.accountName" autocomplete="off" class="layui-input" maxlength="">
									    </div>										   
								  </div>
								</div>
								<div class="layui-form-item" v-show="isfrom==2" style="display:none;">
									<div class="layui-inline">
									    <label class="layui-form-label">当前团队归属</label>
									    <div class="layui-input-inline" >
									        <input type="text" name="teamUserName" disabled="disabled"  placeholder=""  v-model="itemData.teamUserName" autocomplete="off" class="layui-input" maxlength="">
									    </div>										   
								  </div>
								</div>
								<div class="layui-form-item" v-show="isfrom==2" style="display:none;">
									<div class="layui-inline">
									    <label class="layui-form-label" ></label>
									    <div class="layui-input-inline" >
									        <input type="text" name="name" placeholder="请输入分享商姓名或电话"  v-model="name" autocomplete="off" class="layui-input" maxlength="">
									    </div>
									    <div class="layui-form-mid layui-word-aux" @click="getss()">点击搜索</div>
								  </div>
								</div>
								<div class="layui-form-item" v-show="isfrom==2" style="display:none;">
									<div class="layui-inline">
									    <label class="layui-form-label">更换后上级</label>
									    <div class="layui-input-inline">									    
												<select name="accountId" lay-filter="aid" v-model="newaccountId">
													<option  :value="gbData.accountId">{{gbData.nickName}}</option>												 										  
												</select>
											</div>											   
								  </div>
								</div>
								<div class="layui-form-item">
									<div class="layui-input-block">
										<button class="layui-btn layui-btn-normal" lay-submit lay-filter="setmyinfo" @click="confirmOk">确定</button>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>		
		<input type="hidden" id='id'/>
  </div> 
</template>
<script>
export default {	
  name: 'operatoradd',  
  data () {
    return { 
    	id:'',
    	aid:1,
    	curindex:1, //页数 
    	khlist:[],
    	accountId:'',
    	isfrom:1,
    	itemData:{referrer:{}},  		    	
    	gbData:{nickName:''},
    	newaccountId:'', 
    	name:'',
    	fxsid:'',
  	}
  },
  mounted(){ 
  	var _this = this;   	
	  window.getdates = this.getdates;  
  },
  methods: {  	
		getdates:function(data){		
			var _this = this;
			_this.id=data.id;						
			_this.from=data.from;			
			//alert("///"+JSON.stringify(data))
      if(_this.from==1){//创建
      	_this.isfrom=1;
				var url="/crmweb/bapi/v1.9/customer"						
				ajaxRequest(url,'GET','json','',function(ret,err){
					//alert(url+"///"+JSON.stringify(ret))
					  _this.khlist=ret.data.list; 
				})
			}else{//回选信息
				_this.isfrom=2;					
				//alert(url+"///"+JSON.stringify(dates))
				var url="/retail/bapi/v1.9/sharer/"+_this.id	
				ajaxRequest(url,'GET','json','',function(ret,err){
					//alert(url+"///"+JSON.stringify(ret))
					if(ret.data){
					  _this.itemData=ret.data;
					  _this.fxsid=ret.data.id;
					  //_this.getgb(ret.data.id,ret.data.accountName);
					 }
				})
			}
	  },
	  getss:function(){
	  	var _this=this;
	  	var name=_this.name;
	  	if(name){
	  		 _this.getgb();
		  } else {
			  layer.msg('输入的名称或电话号码不能为空');
	  		return false;
		  }
		//   alert(JSON.stringify(name))
	  },
	  getgb:function(){//id,accountName
	  	var _this=this;	  
	  	var dates={				 
				 id:_this.fxsid,
				 name:_this.name,
				 type:1,//1查上级//2查团队归属
			}			
			var url="/retail/papi/v1.9/sharer/superior/name"
			ajaxRequest(url,'GET','json',dates,function(ret,err){
				// alert(url+"///"+JSON.stringify(ret))	
				// if()			 
				if(ret.data == null) {
					layer.msg('没有此用户');
	  		return false;
				}else{
				 _this.gbData=ret.data;
				 _this.newaccountId=ret.data.accountId;
				}
			})
	  	
	  },
		confirmOk:function(){			
			var _this=this;				
			var len = $(".a").length;
			for(var i = 0; i < len; i++) {
				if($(".a").eq(i).val() == ''||$(".a").eq(i).val()<=0) {
					var msg = $(".a").eq(i).attr("placeholder");
					layer.msg('请输入正确的'+msg);
					return;
				}
			}	
			//alert(JSON.stringify(_this.itemData))		
//			if(_this.itemData.mobile!=""){				
//				if(!(/^(?:13\d|15\d|18\d)\d{5}(\d{3}|\*{3})$/.test(mobile))){       
//					layer.msg("请输入正确的手机号"); 
//					return;
//				}
//		  }
//			if(_this.itemData.email!=""){
//				if(!/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(email)){
//					layer.msg("请输入正确的邮箱地址");
//					return;
//				}
//			}	     
	    if(_this.id){//更变上级
	    	if(_this.newaccountId==''){
	      	  layer.msg("请选择更换后的上级");
						return;
	      }
	    	var has='POST';
	    	var url="/retail/bapi/v1.9/change-record"
	     	var dates={
	     		accountId:_this.itemData.accountId,	//用户		
					newReferrerId:_this.newaccountId,	//现上级									
					type:1,
				}	     	
	    }else{
	    	if(_this.accountId==''){
	      	  layer.msg("请选择客户");
						return;
	      }
	    	var has='POST';
	    	var url='/retail/bapi/v1.9/sharer';//开通	
				var dates={					
					 accountId:_this.accountId,				
				}				
			}
		 // alert(JSON.stringify(dates))				 
			ajaxRequest(url,has, 'JSON', dates, function(ret, err){
				//alert(JSON.stringify(dates)+"///"+JSON.stringify(ret))
				layer.msg('操作成功！');
				setTimeout(function(){
			  	var index = parent.layer.getFrameIndex(window.name);	          	
			    parent.layer.close(index);
		      parent.location.reload(); 
	      },1000)
			})
		},
  },
   updated:function(){
		this.isShow = true;
		var _this=this;
		layui.use('form', function(){
		  var form = layui.form;
		  form.render();
		  form.on("select(accountId)", function(data){										
			   _this.accountId = data.value;
			  // alert(_this.accountId)
		  });
		  form.on("select(newaccountId)", function(data){	
		  	//alert(JSON.stringify(data))
			   _this.newaccountId= data.value
		  });
		});
  },

}
</script>
<style scoped>
	.red{color:#ff0000;}
	.layuitj{float:left;line-height:38px;color:#FF0000;}
	.layuitj1{margin-left:20px;}
</style>
